package com.zr.util; /**
 * @author zr
 * @date 2021-02-04 20:36
 */

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public   class  ImageWatermark {

    /**
     * 给图片添加水印
     * @param watermarkFilePath         水印图片路径
     * @param srcImagePath 		源图片路径
     * @param targerImagePath 	        目标图片路径
     * @param degree 			水印图片旋转角度
     */
    public static void addImageWatermark(String watermarkFilePath, String srcImagePath, String targerImagePath, Integer degree) {
        OutputStream os = null;

        if (!srcImagePath.substring(srcImagePath.lastIndexOf("")).equals("db")){


            try {
                Image srcImage = ImageIO.read(new File(srcImagePath));
                String s = srcImage.toString();
                BufferedImage graphics = new BufferedImage(srcImage.getWidth(null), srcImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
                // 得到画布对象
                Graphics2D g = graphics.createGraphics();
//            // 设置对线段的锯齿状边缘处理
//            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//

                // 增加下面代码使得背景透明
                graphics = g.getDeviceConfiguration().createCompatibleImage(srcImage.getWidth(null), srcImage.getHeight(null), Transparency.TRANSLUCENT);
                g= graphics.createGraphics();
                g.drawImage(srcImage.getScaledInstance(srcImage.getWidth(null), srcImage.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
                if (null != degree) {
                    // 设置水印旋转角度及坐标
                    g.rotate(Math.toRadians(degree),  (double) graphics.getWidth() / 2, (double) graphics.getHeight() / 2-400);
                }
                // 水印图象的路径 （如果需要设置透明度需要gif或者png格式的图片  ）
                ImageIcon imgIcon = new ImageIcon(watermarkFilePath);
                // 得到Image对象。
                Image image = imgIcon.getImage();

                // 透明度
                float alpha = 1f;
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

                // 表示水印图片的位置
                g.drawImage(image, 0, graphics.getHeight() / 2-400,null);
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                g.dispose();

                File moveDir = new File(targerImagePath.substring(0,targerImagePath.lastIndexOf("\\")));// 创建目标目录
                if (!moveDir.exists()) {// 判断目标目录是否存在
                    moveDir.mkdirs();// 不存在则创建
                    os = new FileOutputStream(targerImagePath);
                    // 生成图片     (可设置 jpg或者png格式)
                    ImageIO.write(graphics, "png", os);
                }
                os = new FileOutputStream(targerImagePath);
                // 生成图片     (可设置 jpg或者png格式)
//            ImageIO.write(graphics, "png", os);
                ImageIO.write(graphics, "png", os);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void func(File f,TextArea textArea,String logo,String desAddress) {
        File[] p = f.listFiles();
        for (int i = 0; i <p.length ; i++) {
            if(p[i].isDirectory()){
                func(p[i],textArea,logo,desAddress);
            }else {
//                System.out.println(String.valueOf(p[i]).replace("图片已筛选", "图片已筛选(水印)"));
                String str = String.valueOf(p[i]);
                StringBuffer sb = new StringBuffer(str);
                //默认d盘
                if (desAddress.equals("")){
                sb.insert(sb.indexOf(":")+2, "已加水印\\").replace(0,1,"D");
                }else {
                    sb.insert(sb.indexOf(":")+2, "已加水印\\").replace(0,2,desAddress);
                }
                String tar = sb.toString();
//                tar.replace("D:","C:");
//                ImageWatermark.addImageWatermark(logo,p[i].getPath(), tar,0);
                ImageRemarkUtil.setImageMarkOptions(1f,0,0,null,null);
                ImageRemarkUtil.markImageByIcon(logo, str, tar);
                System.out.println("正在处理:"+p[i]);
                textArea.append("正在处理:"+p[i]+"\n");
                textArea.append("存放至:"+tar+"\n");
            }
        }


    }

    public static void main(String args ,TextArea textArea,String logo,String desAddress) {

            File f = new File(args);
            func(f,textArea,logo,desAddress);
            System.out.println("加水印完成!");


    }
}
